﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;

/// <summary>
/// 效果: 点击屏幕在 ARCamera(手机摄像头) 前方 distance 的位置显示一个 spawnedObject 模型
/// </summary>
[RequireComponent(typeof(ARReferencePointManager))]
public class ReferencePointController : MonoBehaviour
{
    public Camera mCamera;
    public GameObject spawnPrefab;
    private GameObject spawnedObject = null;
    private ARReferencePointManager mARReferencePointManager;
    private float distance = 0.5f;

    // Start is called before the first frame update
    void Start()
    {
        mARReferencePointManager = transform.GetComponent<ARReferencePointManager>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount == 0)
            return;
        var touch = Input.GetTouch(0);
        if(spawnedObject == null)
        {
            Vector3 mMenu = mCamera.transform.forward.normalized * distance;
            Pose mPose = new Pose(mCamera.transform.position + mMenu, mCamera.transform.rotation);
            ARReferencePoint mReferencePoint = mARReferencePointManager.AddReferencePoint(mPose);
            spawnedObject = Instantiate(spawnPrefab, mCamera.transform.position + mMenu, mCamera.transform.rotation, mReferencePoint.gameObject.transform);
        }
    }
}
